package com.pkk.blny.task;

import com.pkk.blny.model.domain.Post;
import com.pkk.blny.model.dto.HaloConst;
import com.pkk.blny.service.PostService;
import com.pkk.blny.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author : RYAN0UP
 * @date : 2018/12/5
 */
@Slf4j
public class PostSyncTask {

  /**
   * 将缓存的图文浏览数写入数据库
   */
  public void postSync() {
    PostService postService = SpringUtil.getBean(PostService.class);
    Post post = null;
    int count = 0;
    for (Long key : HaloConst.POSTS_VIEWS.keySet()) {
      post = postService.findByPostId(key).orElse(null);
      if (null != post) {
        post.setPostViews(post.getPostViews() + HaloConst.POSTS_VIEWS.get(key));
        postService.save(post);
        count++;
      }
    }
    log.info("The number of visits to {} posts has been updated", count);
    HaloConst.POSTS_VIEWS.clear();
  }
}
